<template>
    <div class="p-2">

        <el-card shadow="never">
            <template #header>
                <el-row justify="space-between">
                    <el-col :span="1.5">
                        <el-form :model="queryParams" ref="queryFormRef" :inline="true">
                            <el-form-item label="所属公司：" prop="orderPrintStatus">
                                <el-select style="width: 120px;" v-model="queryParams.orderPrintStatus" clearable>
                                    <el-option label="未打印" value="0"></el-option>
                                    <el-option label="已打印" value="1"></el-option>
                                </el-select>
                            </el-form-item>
                            <el-form-item label="客户名称：" prop="workName">
                                <el-input v-model="queryParams.workName" placeholder="请输入"></el-input>
                            </el-form-item>
                            <el-form-item>
                                <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                                <el-button icon="Refresh" @click="resetQuery">重置</el-button>
                            </el-form-item>
                        </el-form>
                    </el-col>
                    <el-col :span="1.5">
                        <span style="color: #000;"> <strong>预付款余额：{{ advancePaymentBalance || 0 }}</strong> </span>
                    </el-col>
                    <el-col :span="1.5"> <el-button @click="handlOpenRecharge" type="warning"> <i
                                class="iconfont icon-daifukuan mr1"></i>
                            新建预付款充值</el-button></el-col>
                </el-row>
            </template>
            <el-table v-loading="loading" :data="dataList">
                <el-table-column label="所属公司" prop="companyName" align="center"></el-table-column>
                <el-table-column label="客户名称" prop="customerName" align="center"></el-table-column>
                <el-table-column label="预付款余额" prop="advanceAmount" align="center"></el-table-column>
                <el-table-column label="预付款详情" align="center">
                    <template #default="scope">
                        <el-button @click="handlOpenPrepaymentDetails(scope.row)" type="primary" link>
                            查看详情
                        </el-button>
                    </template>
                </el-table-column>
                <el-table-column label="预付款退款信息" align="center">
                    <template #default="scope">
                        <el-button @click="hanleOpenRefundInformation(scope.row)" type="primary" link>
                            查看详情
                        </el-button>
                    </template>
                </el-table-column>
                <el-table-column label="操作" align="center" width="120px">
                    <template #default="scope">
                        <el-tooltip content="充值">
                            <el-button link @click="handlOpenRecharge(scope.row)" type="danger">
                                <i style="font-size: 18px;" class="iconfont icon-chehui-1"></i>
                            </el-button>
                        </el-tooltip>
                        <el-tooltip content="退款申请">
                            <el-button link @click="hanleOpenRefund(scope.row)" type="danger">
                                <i style="font-size: 18px;" class="iconfont icon-chehui-1"></i>
                            </el-button>
                        </el-tooltip>
                        <el-tooltip content="删除" placement="top">
                            <el-button @click="handleDelete(scope.row)" :style="{ fontSize: '18px' }" link type="danger"
                                icon="CircleClose"></el-button>
                        </el-tooltip>
                    </template>
                </el-table-column>

            </el-table>
            <pagination v-show="totalData > 0" :total="totalData" v-model:page="queryParams.pageNum"
                v-model:limit="queryParams.pageSize" @pagination="getList" />
        </el-card>

        <!-- 充值预付款 -->
        <RechargePrepayment :visible="rechargeVisible" :initList="rechargeData" :subsidiariesList="subsidiariesList"
            @submit="getList" @close="handleCloseRecharge" />
        <!-- 申请退款 -->
        <ApplyForRefund :visible="applyForRefundVisible" :initList="refundObj" @close="applyForRefundVisible = false" />
        <!-- 预付款退款信息 -->
        <RefundInformation :visible="refundInformationVisible" :initList="refundObj"
            @close="refundInformationVisible = false" @submit="getList" />
        <!-- 查看预付款详情 -->
        <PrepaymentDetails :visible="prepaymentDetailsVisible" :initList="prepaymentDetailsRow"
            :details="prepaymentDetailsData" @close="handleClosePrepaymentDetails" />

    </div>
</template>
<script setup lang="ts">
import { type FormInstance } from 'element-plus';

import { getAdvanceChargeListApi, deleteAdvanceChargeApi } from '@/api/sj/financialManagement/advancePaymentManagement'
import { getAllSubsidiaryApi } from '@/api/sj/warehouse/auxiliary';


/** 充值预付款 */
import RechargePrepayment from './components/rechargePrepayment.vue';
import useRechargePrepayment from '@/hooks/financialManagement/advancePaymentManagement/useRechargePrepayment';

/** 申请退款 */
import ApplyForRefund from './components/applyForRefund.vue';
import useApplyForRefund from '@/hooks/financialManagement/advancePaymentManagement/useApplyForRefund';

/** 预付款退款信息 */
import RefundInformation from './components/refundInformation.vue';

/** 查看预付款详情 */
import PrepaymentDetails from './components/prepaymentDetails.vue';
import usePrepaymentDetails from '@/hooks/financialManagement/advancePaymentManagement/usePrepaymentDetails';

const { rechargeVisible, rechargeData, handlOpenRecharge, handleCloseRecharge } = useRechargePrepayment();
const { applyForRefundVisible, refundObj, refundInformationVisible, hanleOpenRefund, hanleOpenRefundInformation } = useApplyForRefund();
const { prepaymentDetailsVisible, prepaymentDetailsData, prepaymentDetailsRow, handlOpenPrepaymentDetails, handleClosePrepaymentDetails } = usePrepaymentDetails();

const { proxy } = getCurrentInstance() as ComponentInternalInstance;;
const queryFormRef = ref<FormInstance>();

const data = reactive<pageDataOnlyQuery<any>>({
    queryParams: {
        timeDate: '',
        startDate: '',
        endDate: '',
        pageSize: 10,
        pageNum: 1
    }
});

const { queryParams } = toRefs(data);

const loading = ref(false);
const dataList = ref<any>([]);
const totalData = ref(0);
const voucherVisible = ref(false);
const advancePaymentBalance = ref(0);
const subsidiariesList = ref<any>([]);


const handleQuery = () => {
    queryParams.value.pageNum = 1;
    getList();
};

const resetQuery = () => {
    queryFormRef.value?.resetFields();
    queryParams.value.startDate = '';
    queryParams.value.endDate = '';
    queryParams.value.timeDate = '';
    handleQuery();
};

/** 日期选择 */
const handleTimeChange = (val: any) => {
    queryParams.value.startDate = val[0];
    queryParams.value.endDate = val[1];
};

/** 列表删除 */
const handleDelete = (row: any) => {
    ElMessageBox.confirm('删除后此客户的预付款完全清零，您确认要这么做么？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
    })
        .then(async () => {
            try {
                const { code } = await deleteAdvanceChargeApi(row.id);
                if (code == 200) {
                    getList();
                    ElMessage({
                        type: 'success',
                        message: '删除成功!'
                    });
                }
            } catch (error) {

            }

        })
        .catch(() => { });
};

// 撤回
const handleWithdraw = (row: any) => {
    ElMessageBox.confirm('您确认要将此条收款信息撒回么？撤回后此条收入信总将返回到应收款中，已收到的货款也从对应收款卡中撒回，您确认么？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
    })
        .then(() => {
            ElMessage({
                type: 'success',
                message: '删除成功!'
            });
        })
        .catch(() => { });
};

const getList = async () => {
    loading.value = true;
    try {
        const { data } = await getAdvanceChargeListApi(queryParams.value);

        advancePaymentBalance.value = data.advancePaymentBalance;
        dataList.value = data.dataPage.records;
        totalData.value = data.dataPage.total;

        loading.value = false;
    } catch (error) {
        loading.value = false;

    }
};

/** 获取所有子公司 */
const getCompanyList = async () => {
    const { data } = await getAllSubsidiaryApi();
    subsidiariesList.value = data;
};

onMounted(() => {
    getList();
    getCompanyList();
});

</script>
<style lang="scss" scoped></style>